// send_message
exports.send_message = function(req, res) {
	//获取bodyParams参数
	var bodyParams = req.routeInfo.bodyParams;

	//最终结果
	var resultData = {};

	var openidSum = 0;

	var successSum = 0;

	var sendCount = 0;

	var organizationActivityInfo = 0;

	var browsedTimes = 0;

	var connectUrl = 0;

	var start_time = 0;

	var reqUrl = 0;

	var reqType = bodyParams.type;
	var apiType = bodyParams.apiType;

	if(reqType == 0) {

		if(apiType == 'product') {
			reqUrl = "http://www.hxzbi.com:3200/weixin/templateMessagesSent";

		} else {
			reqUrl = "http://demo2.hxzbi.com:3200/weixin/templateMessagesSent";

		}

	} else {

		reqUrl = "http://127.0.0.1:3400/weixin/templateMessagesSent";

	}

	console.log(reqUrl);
	console.log(apiType);

	//任务流创建
	async.waterfall([
		//messageSendRecord
		function(cb) {
			R.SEND_HTTP(
				req, {
					url: CORE_APIConfig.coreAPI.searchOrganizationMessagePushRecord + "/count/1/ompr_type/0/ompr_oid/" + bodyParams.ompr_oid + "/ompr_oaid/" + bodyParams.ompr_oaid,
					method: 'GET'
				},
				function(err, data) {

					sendCount = data.count;

					cb();
				}
			);
		},
		//organizationData
		function(cb) {
			R.SEND_HTTP(
				req, {
					url: CORE_APIConfig.coreAPI.organization_acivity_orgnization + "/oa_id/" + bodyParams.ompr_oaid,
					method: 'POST',
					data: {
						fields: ["oa_title", "oa_start_time", "o_name", "oa_type"].join(",")
					}
				},
				function(err, data) {

					organizationActivityInfo = data.data[0];

					start_time = common.timestampToTime(organizationActivityInfo.oa_start_time);

					if(apiType == 'product') {

						var areaName = "http://www.hxzbi.com/#/";
					} else {

						var areaName = "http://demo2.hxzbi.com/#/";
					}

					if(organizationActivityInfo.oa_type == 0) {
						//bulkPurchase
						connectUrl = areaName + "collage_details?o_id=" + bodyParams.ompr_oid + "&oa_id=" + bodyParams.ompr_oaid + "&oa_title=" + organizationActivityInfo.oa_title

					} else if(organizationActivityInfo.oa_type == 1) {
						//vote
						connectUrl = areaName + "organization_vote_activity_home?o_id=" + bodyParams.ompr_oid + "&oa_id=" + bodyParams.ompr_oaid + "&oa_title=" + organizationActivityInfo.oa_title

					} else if(organizationActivityInfo.oa_type == 2) {
						//bargain
						connectUrl = areaName + "down_price?oa_id=" + bodyParams.ompr_oaid + "&oa_title=" + organizationActivityInfo.oa_title

					} else if(organizationActivityInfo.oa_type == 3) {
						//sign
						connectUrl = ""

					} else if(organizationActivityInfo.oa_type == 4) {
						//leaflet
						connectUrl = ""

					} else if(organizationActivityInfo.oa_type == 5) {
						//discounts
						connectUrl = areaName + "organization_offline_activity?o_id=" + bodyParams.ompr_oid + "&oa_id=" + bodyParams.ompr_oaid + "&oa_title=" + organizationActivityInfo.oa_title

					} else if(organizationActivityInfo.oa_type == 6) {
						//redPackage
						connectUrl = ""

					} else if(organizationActivityInfo.oa_type == 7) {
						//tinyLeaflet
						connectUrl = ""

					}

					cb();
				}
			);
		},
		//browseTimes
		function(cb) {
			if(reqType == 0) {
				R.SEND_HTTP(
					req, {
						url: CORE_APIConfig.coreAPI.searchActivityBrowseRecordFixUser + "/oabr_oaid/" + bodyParams.ompr_oaid,
						method: "POST",
						data: {
							fields: ["oabr_times"].join(",")
						}
					},
					function(err, data) {

						var browsedTimesData = data.data;

						for(var i = 0; i < browsedTimesData.length; i++) {
							browsedTimes += parseInt(browsedTimesData[i].oabr_times)
						}
						console.log(browsedTimesData);
						console.log(browsedTimes);
						cb();
					}
				);
			} else {
				cb()
			}

		},

		function(cb) {
			if(reqType == 0) {
				if(3 - sendCount > 0) {
					cb()
				} else {
					return RES.response(res, false, "可用发送次数不足");
				}
			} else {
				cb()
			}
		},

		//循环发送私信
		function(cb) {
			console.log('循环发送私信');

			var sendArr = bodyParams.send_aim.split("|");

			var sendAim = [];

			for(var i = 0; i < sendArr.length; i++) {
				if(sendArr[i] != "") {
					sendAim.push({
						openid: sendArr[i]
					});
				}
			}

			console.log(sendAim);
			openidSum = sendAim.length;

			var tasks = [];

			sendAim.forEach(function(item, i) {

				if(item.openid != null && item.openid.length > 5) {

					item.openid = item.openid.replace(/\*/g, "-"); //把'is'替换为空字符串
					console.log(item.openid);

					tasks.push(function(cb) {
						if(bodyParams.omrp_title == 0) {
							R.SEND_HTTP(
								req, {
									url: reqUrl,
									method: 'POST',
									data: {
										openid: item.openid,
										type: "zhuanFaYouLi",
										goUrls: connectUrl, //跳转地址
										o_name: organizationActivityInfo.o_name,
										activity_time: start_time, //start time
										content: bodyParams.omrp_content, //start time
									}
								},
								function(err, data) {

									successSum++;

									cb();

								}
							);
						} else if(bodyParams.omrp_title == 1) {
							R.SEND_HTTP(
								req, {
									url: reqUrl,
									method: 'POST',
									data: {
										openid: item.openid,
										type: "huoDongTuiJian",
										goUrls: connectUrl, //跳转地址
										o_name: organizationActivityInfo.o_name,
										oa_name: organizationActivityInfo.oa_title,
										activity_time: start_time, //start time
										user_count: browsedTimes
									}
								},
								function(err, data) {

									if(err) {

									} else {

										successSum++;
										console.log("ok");

									}

									console.log(err);
									console.log(data);

									cb();

								}
							);
						}
					});
				}
			});
			async.waterfall(tasks, cb);
		},
		function(cb) {

			console.log("openid数量：" + openidSum);
			console.log("成功次数：" + successSum);
			R.SEND_HTTP(
				req, {
					url: CORE_APIConfig.coreAPI.addOrganizationMessagePushRecord,
					method: 'POST',
					data: {
						omrp_title: bodyParams.omrp_title,
						omrp_content: bodyParams.omrp_content,
						ompr_arrive_count: successSum,
						ompr_fail_count: openidSum - successSum,
						ompr_oaid: bodyParams.ompr_oaid,
						ompr_oid: bodyParams.ompr_oid,
						ompr_type: reqType,
					}
				},
				function(err, data) {

					//判断是否有错误信息
					if(err) {
						return cb(err, data);
					}

					console.log(data);
					cb();
				}
			);
		}
	], function(err, result) {

		RES.response(res, true, resultData);

	});

}